package io.renren.modules.lanling.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.lanling.service.SchoolService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.lanling.entity.ExamTemplateEntity;
import io.renren.modules.lanling.service.ExamTemplateService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.modules.sys.shiro.ShiroUtils.getUserId;


/**
 * 考试模板表
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-11-06 16:17:26
 */
@RestController
@RequestMapping("lanling/examtemplate")
public class ExamTemplateController {
    @Autowired
    private ExamTemplateService examTemplateService;
    @Autowired
    private SchoolService schoolService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:examtemplate:list")
    public R list(@RequestParam Map<String, Object> params) {

        Long userId = getUserId();
        params.put("createUserId", userId);

        PageUtils page = examTemplateService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lanling:examtemplate:info")
    public R info(@PathVariable("id") Integer id) {
        ExamTemplateEntity examTemplate = examTemplateService.selectById(id);

        return R.ok().put("examTemplate", examTemplate);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:examtemplate:save")
    public R save(@RequestBody ExamTemplateEntity examTemplate) {
        Long userId = getUserId();
        examTemplate.setCreateUserId(userId);
        //查找lanling_school中id
        int schoolId = schoolService.querySchoolId(userId);
        examTemplate.setSchoolId(schoolId);
        examTemplate.setCreateDate(new Date());
        examTemplateService.insert(examTemplate);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:examtemplate:update")
    public R update(@RequestBody ExamTemplateEntity examTemplate) {
        ValidatorUtils.validateEntity(examTemplate);
        examTemplate.setLastModifyDate(new Date());
        examTemplateService.updateAllColumnById(examTemplate);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:examtemplate:delete")
    public R delete(@RequestBody Integer[] ids) {
        examTemplateService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

}
